package com.awesome.livedatademo.livedatadatabinding

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.Button
import android.widget.EditText
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.awesome.common.util.log.ShowLogUtil
import com.awesome.livedatademo.R
import com.awesome.livedatademo.databinding.ActivityDataBindingBinding

/**
 * LiveData + ViewModel + DataBinding
 */
class DataBindingActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 使用DataBinding获取布局
        val binding = DataBindingUtil.setContentView<ActivityDataBindingBinding>(this,R.layout.activity_data_binding)
        binding.lifecycleOwner = this
        // 获取ViewModel对象
        val viewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
        // DataBinding绑定ViewModel
        binding.vm = viewModel
        viewModel.userLiveData.observe(this){
            ShowLogUtil.info(viewModel.userLiveData.value)
        }
        findViewById<Button>(R.id.btn_login).setOnClickListener {
            // 调用viewModel，登录用户
            viewModel.loadUser(findViewById<EditText>(R.id.edit_text).text.toString())
        }
    }
}